package com.yzy.soft.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.collect.Lists;
import com.yzy.soft.common.persistence.dao.TaskMapper;
import com.yzy.soft.common.persistence.model.Task;
import com.yzy.soft.core.base.controller.BaseController;
import com.yzy.soft.core.base.tips.ErrorTip;
import com.yzy.soft.core.base.tips.Tip;
import com.yzy.soft.common.persistence.model.Task;
import com.yzy.soft.system.domain.Percent;
import com.yzy.soft.system.dto.TableDataDto;
import com.yzy.soft.system.freemarker.SynchronousMetadata;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

//import com.yzy.soft.system.dto.TaskDto;

/**
 * 描述：测试控制层
 *
 * @author Yinzhiyuan
 * @date 2018-12-19 01:44:11
 */
@Api(value = "测试控制层")
@RestController
@RequestMapping("/task")
public class TaskController extends BaseController {

    private final static Logger logger= LoggerFactory.getLogger(TaskController.class);

//    @Autowired
//    private ITaskService TaskService;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private SynchronousMetadata synchronousMetadata;

    /**
     * 描述：查询 测试列表
     */
    @ApiOperation(value = "查询测试")
    @GetMapping(value = "/page/{page}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public TableDataDto getAll(@RequestParam(name = "page", defaultValue = "1", required = true) int page, @RequestParam(name = "limit", defaultValue = "10", required = true) int pageSize) throws Exception {

        Wrapper<Task> taskWrapper = new EntityWrapper<Task>();
        page = (page - 1) * pageSize;
        pageSize = page + pageSize;
        List<Task> result = taskMapper.selectPage(new RowBounds(page, pageSize), taskWrapper);

        int i =0;
        for (Task task: result) {
            if(i==0){
                task.setPercent(new Percent("333333"));
            }else if(i==1){
                task.setPercent(new Percent("444444"));
            }else if(i==2){
                task.setPercent(new Percent("555555"));
            }else {
                task.setPercent(new Percent("666666"));
            }
i++;
        }
        TableDataDto tableDataDto = new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(taskMapper.selectCount(taskWrapper));
        tableDataDto.setData(result);
        return tableDataDto;
    }

    /**
     * 描述：根据Id 查询 测试
     *
     * @param id 测试id
     */
    @ApiOperation(value = "查询测试")
    @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Task findById(@PathVariable("id") String id) throws Exception {
        return taskMapper.selectById(id);
    }

    /**
     * 描述：根据用户Id 下拉框要选择的数据库
     *
     * @param id 测试id
     */
//    @ApiOperation(value = "查询测试")
//    @GetMapping(value = "/dropDownDatabase", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public List<Task> selectDropDownDatabaseByUserId() throws Exception {
//        return taskMapper.selectDropDownDatabaseByUserId();
//    }

    /**
     * 描述:创建测试
     *
     * @param TaskDTO 测试DTO
     */
    @ApiOperation(value = "创建测试")
    @PostMapping(value = "" )
    public Tip create(@ModelAttribute Task task) {
        logger.info("TaskController create:{}",task);
        task.setCreatetime(new Date());
        task.setLastupdatetime(new Date());
        Integer result = taskMapper.insert(task);
        return SUCCESS_TIP;
    }

    /**
     * 描述：删除测试
     *
     * @param id 测试id
     */
    @ApiOperation(value = "删除测试")
    @DeleteMapping("/{id}")
    public Tip delete(@RequestParam(name = "ids[]", required = false) Integer[] Task, @PathVariable(name = "id", required = false) Integer id) {
        //Collections.<String>
        logger.info("TaskController delete: {}-------{}", Arrays.toString(Task),id);
        if (null !=id) {
            Integer result = taskMapper.deleteById(id);
        } else if (Task.length != 0) {
            Integer result = taskMapper.deleteBatchIds(Lists.newArrayList(Task));
        }
        return SUCCESS_TIP;
    }

    @ApiOperation(value = "批量删除测试")
    @DeleteMapping(value = "/",produces = MediaType.APPLICATION_JSON_UTF8_VALUE )
    public Tip delete1(@RequestParam(name = "ids", required = false) Integer[] ids) {
//    public Tip delete1(@RequestBody int[] ids) {
        logger.info("TaskController delete1 批量删除测试: {}", Arrays.toString(ids));
        if (ids.length != 0) {
            Integer result = taskMapper.deleteBatchIds(Lists.newArrayList(ids));
        }
        return SUCCESS_TIP;
    }


    /**
     * 描述：更新测试
     *
     * @param id 测试id
     */
    @ApiOperation(value = "更新测试")
    @PutMapping(value = "", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Tip updateTask(@ModelAttribute Task task) throws Exception {
        task.setLastupdatetime(new Date());
        Integer result = taskMapper.updateById(task);
        return SUCCESS_TIP;
    }

//    @GetMapping("/by")
//    public TableDataDto getCadtion(@ModelAttribute Task task) {
//        Wrapper<Task> wrapper = new EntityWrapper<Task>();
//        wrapper.where("company_id={0}", task.getCompanyId());
//        List<Task> result = taskMapper.selectList(wrapper);
//        TableDataDto tableDataDto = new TableDataDto();
//        tableDataDto.setCode("00");
//        tableDataDto.setMsg("000");
//        tableDataDto.setCount(result.size());
//        tableDataDto.setData(result);
//        return tableDataDto;
//    }

    @PutMapping("/status/{id}")
    public Tip status(@PathVariable("id") Integer id) {
        Integer result = taskMapper.status(id);
        if (null == result) {
            return new ErrorTip(500, "s");
        }
        return SUCCESS_TIP;
    }

    @GetMapping("/taskId/{taskId}")
    public TableDataDto getbyAccountId(@PathVariable(name = "taskId") String taskId, @RequestParam(name = "page", defaultValue = "1", required = true) int page, @RequestParam(name = "limit", defaultValue = "10", required = true) int pageSize) {
        Wrapper<Task> taskWrapper = new EntityWrapper<>();
        taskWrapper.where("accountid={0}", taskId);
        page = (page - 1) * pageSize;
        pageSize = page + pageSize;
        List<Task> result = taskMapper.selectPage(new RowBounds(page, pageSize), taskWrapper);
        TableDataDto tableDataDto = new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(taskMapper.selectCount(taskWrapper));
        tableDataDto.setData(result);
        return tableDataDto;
    }

    @ApiOperation("批量将费用归档设置成弃用状态")
    @PostMapping("/batchNuUse")
    public Tip batchNuUse(@RequestParam(name = "ids[]", required = false) Integer[] Task) {
        if (Task.length != 0) {
            Integer result = taskMapper.batchNuUseIds(Lists.newArrayList(Task));
        }
        return SUCCESS_TIP;
    }

    @ApiOperation("批量将费用归档设置成可用状态")
    @PostMapping("/batchUse")
    public Tip batchUse(@RequestParam(name = "ids[]", required = false) Integer[] Task) {
        if (Task.length != 0) {
            Integer result = taskMapper.batchUseIds(Lists.newArrayList(Task));
        }
        return SUCCESS_TIP;
    }







}